Use case: UART communication in polling mode

Sequence diagram

UART processing in polling mode

@startuml
participant "Application" as p1
participant "HAL UART driver" as p2

== initialization and configuration ==

== UART Processing in polling mode ==
alt polling
p1->p2: HAL_UART_Transmit(&huart,\n                                aTxBuffer,\n                                DATA_TO_SEND_SIZE,\n                                100)
note over p2 #00FF00
Transmitting data ongoing
end note

p2-->p1: <color #Green> HAL_OK
else
p1->p2: HAL_UART_Receive(&huart,\n                                aRxBuffer,\n                                DATA_TO_RECEIVE_SIZE,\n                                100)
note over p2 #00FF00
Receiving data ongoing
end note
p2-->p1: <color #Green> HAL_OK
end

@enduml

Code snippet

HAL1

HAL2

#define DATA_TO_SEND_SIZE           256
#define DATA_TO_RECEIVE_SIZE       DATA_TO_SEND_SIZE

uint8_t aTxBuffer[] = " **** UART Communication **** ";
uint8_t aRxBuffer[DATA_TO_RECEIVE_SIZE];

UART_HandleTypeDef huart;


HAL_UART_Init(&huart)




HAL_UART_Receive(&huart, aRxBuffer,
                      DATA_TO_RECEIVE_SIZE, 100)


HAL_UART_Transmit(&huart, aTxBuffer,
                      DATA_TO_SEND_SIZE, 100)
#define DATA_TO_SEND_SIZE           256
#define DATA_TO_RECEIVE_SIZE       DATA_TO_SEND_SIZE

uint8_t aTxBuffer[] = " **** UART Communication **** ";
uint8_t aRxBuffer[DATA_TO_RECEIVE_SIZE];

hal_uart_handle_t huart;
hal_uart_config_t uart_config;

HAL_UART_Init(&huart, HAL_UART4)

/* add config for uart instance */
HAL_UART_SetConfig(&huart, &uart_config)

HAL_UART_Receive(&huart, aRxBuffer,
                      DATA_TO_RECEIVE_SIZE, 100)


HAL_UART_Transmit(&huart,aTxBuffer,
                      DATA_TO_SEND_SIZE, 100)

Key changes

  1. In HAL2, the UART instance is provided to HAL_UART_Init() and configuration is separated using HAL_UART_SetConfig().